﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using DroidClaim.Web.Mapper;
using DroidClaim.Web.Models;
using DroidClaim.Web.Service;
using DroidClaim.Web.Utilities;

namespace DroidClaim.Web.Controllers
{
    public class ClaimInfoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ListClaimInfo()
        {
            ListClaimInfoModel model = new ListClaimInfoModel();      
             SetupModelToViewInList(model);
            if (Request.QueryString["sort"] != null && Request.QueryString["sortdir"] != null)
            {
                GetAllClaimInfoResponseModel temp = Search(new GetAllClaimInfoRequestModel()
                {
									 Code = "",
												 Name = "",
						                    Sort = Request.QueryString["sort"].ToString(),
                    SortType = Request.QueryString["sortdir"].Equals("asc") ? sType.ASC : sType.DESC
                });
                model.ListOfClaimInfo = temp.List;
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult ListClaimInfo(string submitButton, string[] checkbox, ListClaimInfoModel input)
        {
            if (ModelState.IsValid)
            {
               switch(submitButton) {
                   case "Search" :
                        input.ListOfClaimInfo = PrepareSearch(input);                      
                        break;
                   default :
                        break;
               }
            }
            SetupModelToViewInList(input);
            return View(input);
            
        }

        public ActionResult EditClaimInfo(String Code)
        {
            ServiceClient service = new ServiceClient();

            GetAllClaimInfoRequestDto objGet = new GetAllClaimInfoRequestDto()
            {
                Code = Code
            };

            var list = service.GetAllClaimInfo(objGet);
			ClaimInfoModel resultModel = null;


			if (list.Data != null && list.Data.Count > 0)
			{
				ClaimInfoDto resultDto = list.Data[0];
				resultModel = ClaimInfoMapper.DtoToModel(resultDto);
			}
			SetupRegisterViewModel(resultModel);
            return View(resultModel);
		}

		public ActionResult Update(ClaimInfoModel inputModel)
		{
            if (ModelState.IsValid)
            {
                ServiceClient service = new ServiceClient();
                service.UpdateClaimInfo(ClaimInfoMapper.ModelToDto(inputModel));
                return RedirectToAction("ListClaimInfo", "ClaimInfo");
            }   
            else
            {
                SetupRegisterViewModel(inputModel);
                return View("EditClaimInfo", inputModel);
            }
			
		}

        public ActionResult Add()
        {
            ClaimInfoModel model = new ClaimInfoModel();
            SetupRegisterViewModel(model);
            return View("AddClaimInfo", model);
        }

        [HttpPost]
        public ActionResult AddClaimInfo(ClaimInfoModel inputModel)
        {
            if (ModelState.IsValid)
            {
                ServiceClient service = new ServiceClient();
                service.AddClaimInfo(ClaimInfoMapper.ModelToDto(inputModel));
                return RedirectToAction("ListClaimInfo");
            }
            else
            {
                SetupRegisterViewModel(inputModel);
                return View(inputModel);
            }
           
        }

        [HttpPost]
        public ActionResult DeleteClaimInfos(string[] checkbox)
        {
            ListClaimInfoModel input = new ListClaimInfoModel();
            if (ModelState.IsValid)
            {
                input = Delete(checkbox, input);
            }

            SetupModelToViewInList(input);
            return View("ListClaimInfo", input);
        }

        [HttpPost]
        public String DeleteClaimInfoById(string Id)
        {
            List<string> delList = new List<string>();
            delList.Add(Id);
            ServiceClient service = new ServiceClient();
            service.DeleteClaimInfo(delList);

			
			return "Success";
           
        }

        // Check does Code exist
        [HttpPost]
        public JsonResult doesCodeExist(string Code, string Mode, string OldValue)
        {
			bool callCheck = false;
            if (Mode.Equals("Add", StringComparison.OrdinalIgnoreCase))
            {
                callCheck = true;
            }
            else if (Mode.Equals("Edit", StringComparison.OrdinalIgnoreCase))
            {
                if (!OldValue.Trim().Equals(Code.Trim(), StringComparison.OrdinalIgnoreCase))
                    callCheck = true;
            }

            if (callCheck)
            {
                GetAllClaimInfoResponseModel temp = Search(new GetAllClaimInfoRequestModel()
                {
					
				Code = Code,Name = "",                    Sort = "Code",
                    SortType = sType.ASC
					
                });
                return Json(temp.List.Count == 0);
            }
            else
                return Json(true);
        }

		#region private methods

        private void SetupRegisterViewModel(ClaimInfoModel model)
        {
            Utilities.Utilities.PrepareDateTimeProperity(model);
        }

		private void SetupModelToViewInList(ListClaimInfoModel model)
        {
           //TODO : ออกแบบไว้รองรับการแสดง message หลัง Add, Edit, Delete
        }

        private ListClaimInfoModel Delete(string[] inputList, ListClaimInfoModel input)
        {
            List<string> delList = new List<string>();
            if (inputList != null && inputList.Length > 0)
            {
                foreach (string id in inputList)
                {
                    delList.Add(id);
                }
                ServiceClient service = new ServiceClient();
                var resEmp = service.DeleteClaimInfo(delList);
            }

            input.ListOfClaimInfo = PrepareSearch(input);
            return input;
        }

        private List<ClaimInfoModel> PrepareSearch(ListClaimInfoModel input)
        {

            GetAllClaimInfoRequestModel searchInputs = new GetAllClaimInfoRequestModel()
            {
			Code = (input.CodeSearch == null) ? "" : input.CodeSearch,Name = (input.NameSearch == null) ? "" : input.NameSearch,                Sort = "Code", SortType = sType.ASC
            };
            GetAllClaimInfoResponseModel result = Search(searchInputs);
            return result.List;
        }

        private GetAllClaimInfoResponseModel Search(GetAllClaimInfoRequestModel input)
        {
            ServiceClient service = new ServiceClient();
            GetAllClaimInfoRequestDto gAllreq = new GetAllClaimInfoRequestDto { 
		  			      Code = input.Code,
		  			      Name = input.Name,
		                   SortType = input.SortType.ToString(),
                 Sort = input.Sort
            };
            var response = service.GetAllClaimInfo(gAllreq);
            GetAllClaimInfoResponseModel result = new GetAllClaimInfoResponseModel();
            result.List = new List<ClaimInfoModel>();
            if(response != null)
            {
                if (response.Data != null && response.Data.Count > 0)
                {
                    foreach (ClaimInfoDto responseDTO in response.Data)
                    {
                        ClaimInfoModel resultModel = null;
                        ClaimInfoDto tempDto = responseDTO;
                        resultModel = ClaimInfoMapper.DtoToModel(tempDto);
                        result.List.Add(resultModel);
                    }
                }
            }
            return result;
        }
		#endregion
    }
}
